Ext.onReady(function(){
	//结算方式
	var statementTypeList = [
         {"value":"1","text":"现金"},
         {"value":"2","text":"支票"},
         {"value":"3","text":"汇票"},
         {"value":"4","text":"转账"},
         {"value":"5","text":"POS转账"},
         {"value":"6","text":"支付宝"},
         {"value":"7","text":"银联"},
         {"value":"8","text":"银行转入"},
         {"value":"9","text":"银行按揭"}
     ];
	var statementTypeStore = Ext.data.Store({
    	autoDestroy: true,
        fields :["value","text"],
        data : statementTypeList
    });
    
    //本次应收意向总金额
    var canReceiveMoney = 0;
	
	/** 认筹信息开始 **/
	var depDetailPanel = Ext.create("Ext.form.Panel", {
        title: "认筹信息",
        layout: "fit",
        renderTo: Ext.getBody(),
        items: {
            xtype: "form",
            bodyPadding:5,
            border: false,
            layout: "form",
            fieldDefaults: {
                labelWidth: 80,
                labelAlign: "right",
                margin: "0 0 10 0"
            },
            items: [{
                xtype: "fieldcontainer",
                layout: "hbox",
                items: [{
                	xtype: 'displayfield',
		            name: 'info_depositCode',
		            fieldLabel: '认筹号'
                }]
      		},
         	{
                xtype: "fieldcontainer",
                layout: "hbox",
                items: [{
                	xtype: "displayfield",
                	name: "info_receivable",
                	fieldLabel: "应收意向金"
            	},
                {
                	xtype: "displayfield",
                	name: "info_received",
                	fieldLabel: "已收意向金"
            	}]
          	},
         	{
         		xtype: "fieldcontainer",
                layout: "hbox",
                items: [{
	                xtype: "textarea", height: 100, width: 475, readOnly:true,
	                name: "info_remark",
	                fieldLabel: "备注",
	                emptyText: ""
                }]
            }]
        }
    });
    //加载认筹信息
    depDetailPanel.getForm().load({
	    url: 'getPayDepositById',
	    params: {"id": relationId},
	    success: function(form, action){
	    	//总认筹金额
	    	var info_receivable = action.result.data.info_receivable;
	    	//已收认筹金额
	    	var info_received = action.result.data.info_received;
	    	//收款表单,设置收款最大值
	    	canReceiveMoney = info_receivable - info_received;
	    	depReceivePanel.getForm().findField("realAmount").setMaxValue(canReceiveMoney);
	    	depReceivePanel.getForm().findField("realAmount").setValue(canReceiveMoney);
	    	
	    	//收款信息,添加结算人员
	    	depReceivePanel.getForm().findField("userName").setValue(action.result.data.userName);
	    	depReceivePanel.getForm().findField("userId").setValue(action.result.data.userId);
	    	depReceivePanel.getForm().findField("userCode").setValue(action.result.data.userCode);
	    	depReceivePanel.getForm().findField("relationId").setValue(relationId);
	    	
	    },
	    failure: function(form, action) {
	        Ext.Msg.alert("加载失败", action.result.errorMessage);
	    }
	});
    /** 认筹信息结束 **/
    
    /** 收款信息开始 **/
	//上传图片展示框
	var showImgPanel = Ext.create('Ext.Panel', {
        width:600,
       	height:120,
        frame: true,
        collapsible: false,
      	autoScroll:true,
      	bodyStyle: 'overflow-y:hidden; overflow-x:auto',
        layout: "hbox"
    });
	
	//收款附件表单
	var ajaxFileUploadForm = Ext.create("Ext.form.Panel", {
		border: false,
		items: [{
            xtype: 'filefield',
            fieldLabel: "上传凭证",
            buttonText: "上传凭证",
            name: "file",
            layout: "hbox",
            validator: function(value){
            	if(value){
            		if(!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG|BMP)$/.test(value)){
			      		return "图片格式不正确!";
				    }else{
				    	return true;
				    }
            	}else{
			    	return true;
			    }
			},
            listeners: {
        		change: function(file){
        			var form = ajaxFileUploadForm.getForm();
        			if(form.isValid()){
	        			form.submit({
		                    url: jypath + '/common/file/extUploadFile?type=1',
		                    waitMsg: '附件上传中,请稍候...',
		                    success: function(form, action) {
		                    	var fileName = action.result.fileName;
		                    	var httpUrl = action.result.url;
		                    	var imgs="<a href='"+httpUrl+"' target='_blank'><img style='width: 80px; height: 90px;' title='"+fileName+"' src='"+httpUrl+"'/></a>";
		                    	var imgPanel = Ext.create("Ext.Panel", {
							        html: imgs
		                    	})
								showImgPanel.add(imgPanel);
								//添加隐藏字符串
								var fileArr = depReceivePanel.getForm().findField("fileArr").getValue();
								if(!fileArr){
									fileArr = httpUrl;
								}else{
									fileArr = fileArr + "|" + httpUrl;
								}
								depReceivePanel.getForm().findField("fileArr").setValue(fileArr);
								Ext.getCmp("certification").show();
			       			},
			       			failure: function(form, action){
	                        	Ext.Msg.alert('Failure', 'Your photo "' + action.result.fileName + '" has been uploaded.');
			       			}
		                });
        			}
        		}
        	}
        },{
        	xtype: "fieldcontainer",
        	layout: "hbox",
        	fieldLabel: "凭证附件",
        	id: "certification",
        	name: "certification",
        	hidden: true,
        	items: [showImgPanel]
        }]
	});

	var depReceivePanel = Ext.create("Ext.form.FormPanel", {
        title: "收款信息",
        layout: "fit",
        margin: "10 0 0 0",
        renderTo: Ext.getBody(),
        items: {
            xtype: "form",
            bodyPadding:5,
            border: false,
            layout: "form",
            fieldDefaults: {
                labelWidth: 80,
                labelAlign: "right",
                margin: "0 0 10 0"
            },
            items: [
            	/* 内部转账功能不确定,留待备用
            	 * 功能未全部实现,启用后需继续完成
            	{
                xtype: "grid",
                margin: "0 0 20 30",
                scrollOffset: 0,
                forceFit: true,
                width: document.body.clientWidth*0.9,
                columns: [
	                { header: '业务编号',  dataIndex: 'depositCode' },
			        { header: '房号', dataIndex: 'roomId'},
			        { header: '客户名称', dataIndex: 'customerName' },
			        { header: '房款类型', dataIndex: 'moneyType' },
			        { header: '业务标志', dataIndex: 'businessType' },
			        { header: '可转金额', dataIndex: 'changeMoney' }
                ],
                selType: 'checkboxmodel',
                store: Ext.create('Ext.data.ArrayStore', {}),
                tbar : [{
					text : '内部转账',
					icon : jypath + '/resources/ExtJSicons/book_add.png',
					handler : function() {
						var internalTransferWin=Ext.create('component.proceeds.internalTransferWin');
           		 	    internalTransferWin.show();
					}
				}]
      		},
      		*/
         	{
                xtype: "fieldcontainer",
                layout: "hbox",
                items: [{
                	xtype : 'textfield', fieldLabel : '结算人员ID',
			    	name: "userId", readOnly: true, hidden: true
                },{
                	xtype : 'textfield', fieldLabel : '结算人员Code',
			    	name: "userCode", readOnly: true, hidden: true
                },{
                	xtype : 'textfield', fieldLabel : '认筹编号',
			    	name: "relationId", readOnly: true, hidden: true
                },{
                	xtype : 'textfield', fieldLabel : '收款类型',
			    	name: "businessType", readOnly: true, hidden: true,
			    	value: 0
                },{
                	xtype : 'textfield', fieldLabel : '收款凭证',
			    	name: "fileArr", readOnly: true, hidden: true
                },{
                	xtype: "numberfield",
                	fieldLabel: "收款金额",
                	name: "realAmount",
                	allowBlank:false,
                	hideTrigger:true,
                	blankText: "收款金额不能为空!",
                	minValue: 1,
                	listeners: {
                		change: function(field){
                			var totalRecField = depReceivePanel.getForm().findField("totalRec")
                			totalRecField.setValue("<font style='color: red; font-size: 30px;'>"+Ext.util.Format.number(field.getValue(), "0,000.00") +"元</font>");
                		}
                	}
            	},
                {
                	xtype: "displayfield",
                	name: "totalRec",
                	fieldLabel: "本次收款总额"
            	}]
          	},
         	{
	        	xtype: "fieldcontainer",
	            layout: "hbox",
	            items: [{
	                xtype: "textfield",
	        		fieldLabel: "票据号码",
	        		name: "billCode",
	        		maxLength: 100,
	        		width:475
	            }]
        	},
        	{
				xtype: "fieldcontainer",
	            layout: "hbox",
	            items: [{
	                xtype : 'combobox',
					fieldLabel : '结算方式',
					store: statementTypeStore,
			    	displayField: "text",
			    	valueField: "value",
			    	allowBlank: false,
			    	blankText: "结算方式不能为空!",
			    	id: "statementType",
			    	name: "statementType",
			    	editable: false,
			    	listeners: {
			    		select: function(combo,record, index){
			    			//当选中数据为 POS转账 时,隐藏/显示转账手续费字段
			    			//动态控制手续费字段是否可以为空
			    			var value = combo.getValue();
		    				var serviceFeeField = depReceivePanel.getForm().findField("serviceFee");
			    			if(value == 5){
	                    		serviceFeeField.show();
	                    		//serviceFeeField.allowBlank=false;
	                    		//serviceFeeField.blankText="POS转账,手续费不能为空";
			    			}else{
			    				serviceFeeField.setValue(null);
			    				serviceFeeField.allowBlank=true;
	                    		serviceFeeField.blankText="";
			    				serviceFeeField.hide();
			    			}
			    		}
			    	}
	            },{
                	xtype: "numberfield",
                	fieldLabel: "转账手续费",
                	name: "serviceFee",
                	hidden: true,
                	hideTrigger:true,
                	minValue: 0
            	}]
        	},{
        		xtype: "fieldcontainer",
                layout: "hbox",
                items: [{
                	xtype : 'textfield',
					fieldLabel : '结算人员',
			    	name: "userName",
			    	readOnly: true
                },{
                	xtype : 'datefield',
                	name: "statementDate",
					fieldLabel : '结算日期',
					format: 'Y-m-d',
					value: new Date(),
					maxValue: new Date()
                }]
        	},{
                xtype: "fieldcontainer",
	            layout: "hbox",
	            items: [{
	                xtype: "textarea",
	                name: "remark",
	        		height: 100,
	                fieldLabel: "备注",
	                width:475
	            }]
        	},{
        		xtype: "fieldcontainer",
	            layout: "hbox",
	            items: [ajaxFileUploadForm]
        	}],
        	buttonAlign: "left",
        	buttons: [{
                text:'保存',
                handler: function(){
		        	var form = depReceivePanel.getForm();
				    if(form.isValid()){
				    	// 发送请求
				    	var waiting = Ext.Msg.wait('正在处理，请稍等...','','');
						Ext.Ajax.request({
							method: "POST",
							url: "proceeds",
							params: form.getValues(),
							success: function(response, options){
								if(waiting != null){
									waiting.hide();
								}
								var jsonResult = Ext.decode(response.responseText);
								if(jsonResult.success==true){
											 Ext.MessageBox.alert("系统提示", "保存成功! ", function(but) {  
			    								   var toUrl =jypath+"/customer/payDeposit/toView?id="+relationId+"&returnUrl=,marketing,pin,index";
			    								   window.location.href = toUrl;
			    				                }); 
								}else{
									Ext.Msg.alert('失败', jsonResult.errorMessage);
								}
							},
							failure: function(response, options){
								Ext.Msg.alert('错误', '请求超时.');
							}
						});
					}
            	}
            },{
            	 text: '返回',
                 handler: function(){
                 	var tabKey = "dep_pay"+relationId;
                 	parent.TabControlRemove(tabKey);
      			}
            }]
        }
    });
    /** 收款信息结束 **/
});




